<?php use_helper('I18N', 'Date') ?>
<?php include_partial('product/assets') ?>

<script type="text/javascript">
	function onDownloadTypeChange()
	{
		var isCreateThubnail = 0;	
		if($("#sel_download_types").find("option:selected").text() == "Product picture")
		{
			isCreateThubnail = 1;
		}
		swfu.setPostParams({"isCreateThumbnail":isCreateThubnail});
	}

	function getSubCategory(level)
	{
		$("#div_category>select").each(function(){
				var temp_id = $(this).attr("id");
				var temp_array = temp_id.split("_");
				var temp_level = temp_array[3];
				if(temp_level>level)
				{
					$(this).remove();
				}
			});

		
		parent_id=$("#product_category_id_"+level).val();
		 var url = $("#category_url").val();
		 var data = {"parent_id":parent_id,"level":level};
	    $.post(
		        url,
		        data,
		        function(data) {
			        $("#div_category").append($(data))
		        });			 
	}

	function onProductTypeChange()
	{
		if($("#product_product_type_id").find("option:selected").text() == "IMAGES" ||
		   $("#product_product_type_id").find("option:selected").text() == "Packaging Images" ||
		   $("#product_product_type_id").find("option:selected").text() == "Images-Back View" ||
		   $("#product_product_type_id").find("option:selected").text() == "Images-Front View")
		{
			$("#hid_is_special").val(1);
			swfu.setPostParams({"isSpecial":1});
			$("#div_product_name").css("display","none");
			$("#sel_download_types").css("display","none");
			return;
		}

		$("#hid_is_special").val(0);
		swfu.setPostParams({"isSpecial":0});
		$("#div_product_name").css("display","block");
		$("#sel_download_types").css("display","inline-block");
		
		var produc_type_id = $("#product_product_type_id").val();
	    var url = $("#hid_url").val();
	    var data = {"product_type_id":produc_type_id};
	    $("#sel_download_types").attr("disabled",true);
	    $.post(
	        url,
	        data,
	        function(data) {
	        	$("#sel_download_types").attr("disabled",false);
		       $("#sel_download_types").html(data);

		       $("#sel_download_types option").each(function(){
					if($(this).html() == "Product picture")
					{
						$(this).attr("selected","selected")
					}
			       });
		
	        });		
	}
</script>

<div id="sf_admin_container">
  <h1><?php echo __('New Product', array(), 'messages') ?></h1>

  <?php include_partial('product/flashes') ?>

  <div id="sf_admin_header">
  </div>

  <div id="sf_admin_content">
	<div class="sf_admin_form">
	  <input type="hidden" id="category_url" name="category_url" value="<?php echo url_for('product/getCategoryListByParentId')?>"/>
	  <input type="hidden" id="hid_url" value="<?php echo url_for("product/getDownloadTypeByProductTypeId")?>"/>
	  <form action="<?php echo url_for('product/new')?>" method="post">    
	  	<input type="hidden" id="hid_is_special" name="hid_is_special" value="0"/>
	  	<fieldset id="sf_fieldset_none">
	        <div class="sf_admin_form_row sf_admin_foreignkey sf_admin_form_field_category_id">
	        	<div>
	      			<label>Category</label>
	      			<div class="content" id="div_category">
	      				<select id="product_category_id_0" name="product_category_ids[]" onchange="getSubCategory(0)">
	      					<option value="">Select</option>
	      					<?php foreach ($categoryList as $category):?>
	      						<option value="0_<?php echo $category->getId();?>"><?php echo $category->getName();?></option>
	      					<?php endforeach;?>
						</select>
					</div>
	
	          </div>
	  		</div>
	        <div class="sf_admin_form_row sf_admin_foreignkey sf_admin_form_field_product_type_id">
	        	<div>
		      		<label>Product type</label>
		      		<div class="content">
		      			<select id="product_product_type_id" name="product_product_type_id" onchange="onProductTypeChange()">
	      					<option value="">Select</option>
	      					<?php foreach ($productTypeList as $productType):?>
	      						<option value="<?php echo $productType->getId();?>"><?php echo $productType->getName();?></option>
	      					<?php endforeach;?>		      			
						</select>
					</div>
	          	</div>
	  		</div>
	    	<div class="sf_admin_form_row sf_admin_text sf_admin_form_field_name" id="div_product_name">
	        	<div>
	      			<label>Name</label>
	      			<div class="content">
	      				<input type="text" id="product_name" name="product_name">
	      			</div>
	          	</div>
	  		</div>
		   <div class="sf_admin_form_row sf_admin_text sf_admin_form_field_name">
		   	<div>
		      <label>Files</label>
		      <div>
		      <select onchange="onDownloadTypeChange()" id="sel_download_types" disabled="disabled">
		      	<option value="">Select</option>
		      	<?php if(isset($availableDownloadTypes) && $availableDownloadTypes != null):?>
		      	<?php foreach($availableDownloadTypes as $downloadType):?>
		      	<option value="<?php echo $downloadType->getId(); ?>"><?php echo $downloadType->getName();?></option>
		      	<?php endforeach;?>
		      	<?php endif;?>
		      </select>
		        <?php 
		        	$isCreateThumbnail=false;
		        	$filesLimit = 0;		
		        ?>
		      	<?php include sfConfig::get("sf_web_dir").'/_upload.php';?>
		      </div>
		      </div>
		    </div>		  		
	      </fieldset>
	    <ul class="sf_admin_actions">
	    	<?php echo $helper->linkToList(array(  'params' =>   array(  ),  'class_suffix' => 'list',  'label' => 'Back to list',)) ?>  
	    	<li class="sf_admin_action_save"><input type="submit" value="Save"></li>  
	    	<li class="sf_admin_action_save_and_add"><input type="submit" name="_save_and_add" value="Save and add"></li>
	    </ul>
	  </form>
	</div>
  </div>

  <div id="sf_admin_footer">
  </div>
</div>
